Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Change outer ul background color when inner li has a class="selected"

I have some menus with unordered list (ul):

<ul class="myul">
  <li>Button1</li>
  <li>Button2</li>
  <li class="selected">Button3</li>
  <li>Button4</li>
</ul>
<ul class="myul">
  <li>Button5</li>
  <li>Button6</li>
  <li>Button7</li>
  <li>Button8</li>
</ul>

and so on...

How i can change with jQuery the background color of outer ul when the inner li has class="selected"?

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In this case, you can use method closest() or parent(). Here is an example using method closest().

Method closest() target a parent of any level relative to a child.

Method parent() only target the first level parent.

This is a jQuery solution:

$('li.selected').closest('ul').css('background', 'green');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<ul class="myul">
    <li>Button1</li>
    <li>Button2</li>
    <li class="selected">Button3</li>
    <li>Button4</li>
</ul>

<ul class="myul">
    <li>Button5</li>
    <li>Button6</li>
    <li>Button7</li>
    <li>Button8</li>
</ul>

about 4 years ago · Juan Pablo Isaza Report

0

This would be a job for the "has" selector in CSS. Unfortunately, as of this writing, there's almost no browser support for it. Safari Technology Preview "cutting edge" browser has support, and that's just about it.

https://caniuse.com/?search=has

You can search for "css parent selector" to see how this has been a longstanding request among many web developers.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!